<?php

if(!defined('IN_SYS')) exit('Access Denied');

class catch_controller extends admincp{

	public function __construct(){
		parent::__construct();
	}
	
	public function index(){ 
		$where = '';
		if(!empty($this->currentYumingId)){
			$where = " or yuming_id='".$this->currentYumingId."' ";
		}
		$pg = new page("select * from catch where yuming_id=0 $where  order by id desc");
		$list = $pg->get_list(20);
		$page = $pg->get_page();
		$auto=db::getfield("select autorule from yuming where id='".$this->currentYumingId."' ");
		require self::tpl('catch');
	}

	public function add(){
		if(IS_POST){
			$v = gp("v",false);
			$v['yuming_id'] = gp("yuming_id");
			if(isset($v['scope'])){
				$time=time();
				db::query("insert into catch (yuming_id,class,listurl,articlerule,titlerule,contentrule,norule,updatetime ) 
					values ('$v[scope]', '$v[class]', '$v[listurl]', '$v[articlerule]','$v[titlerule]','$v[contentrule]','$v[norule]','$time' )");
                $sid = db::insert_id();
                $sql = "CREATE TABLE `caiji_{$sid}` (
                `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
                `title` varchar(500) NOT NULL DEFAULT '',
                `content` longtext NOT NULL,
                `class` varchar(500) NOT NULL DEFAULT '',
                `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态:0未处理，1已处理，2处理失败',
                `yuming_id` int(11) unsigned NOT NULL DEFAULT '0',
                PRIMARY KEY (`id`),
                KEY `idx_yuming_id` (`yuming_id`)
              ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;";
                db::query($sql);
				self::json( array('status' => 1, 'msg' => '添加成功', 'redirect' => '?m=catch&a=index&yuming_id='.$v['yuming_id']));
			}else{
				self::json(array('status'=>1,'msg'=>'请先选择作用范围'));
			}
		}else{
			$classlist = false;
			if ($this->currentYumingId) {
				$classlist = db::select("select c.* from classlist as c where c.yuming_id='".$this->currentYumingId."' order by c.weight,id");
			}
			require self::tpl("catch_add");
		}
	}

	public function edit(){      
		$id = gp('id');
		if(IS_POST){
			$time = time();
			$v = gp("v",false);
			$v['yuming_id'] = gp("yuming_id");
			if ($v['scope']==0) {
				$v['class'] = '';
			}
			db::query("update catch set yuming_id='$v[scope]', class='$v[class]', listurl='$v[listurl]',articlerule='$v[articlerule]' ,titlerule='$v[titlerule]',contentrule='$v[contentrule]' ,norule='$v[norule]',updatetime='$time'  where id='$id' ");
			self::json( array('status' => 1, 'msg' => '修改成功', 'redirect' => '?m=catch&&a=index&yuming_id='.$v['yuming_id']));
		}else{
			$res = db::find("select * from catch  where id='$id' ");
			$classlist = false;
			if ($this->currentYumingId) {
				$classlist = db::select("select c.* from classlist as c where c.yuming_id='".$this->currentYumingId."' order by c.weight,id");
			}
			require self::tpl("catch_edit");
		}
	}

	public function del(){
		$yuming_id=gp("yuming_id");
		$id = gp('id');
		db::query("delete from catch where id='$id' ");
		self::redirect('?m=catch&a=index&yuming_id='.$yuming_id, "删除成功");
	}

	// 自动采集
	public function switchcaiji(){
		$data = array();
		$data['version'] = $GLOBALS['G']['softversion'];
		$data['appid'] = $GLOBALS['G']['appid'];
		$data['http_host'] = $_SERVER['HTTP_HOST'];
		$data['ar'] = 1;
		$data['autoRule'] = gp("data");
		$data['allinone'] = 1;	//新版本使用一次性触发新闻采集
		$yuming_id = gp("yuming_id");
		$checkrule = 1;
		if ($data['autoRule']==1) {
			$checkrule = db::getfield("select count(*) from catch where yuming_id='$yuming_id'");
		}
		
		//如果已经设置了规则
		if ($checkrule || $data['autoRule']==0) {
			$webhost=db::find("select * from yuming where id='$yuming_id'");
			$data['webhost'] = $webhost['name'];
			self::json( array('status' => 1, 'msg' => '创建成功', 'redirect' => '?m=yuming') );
		} else {
			self::json( array('status' => 0, 'msg' => '该站点下有至少一个单站点规则才可以开启', 'redirect' => '?m=yuming') );
		}
		
	}

	public function getCaiji(){
		$data = array();
		$data['version'] = $GLOBALS['G']['softversion'];
		$data['appid'] = $GLOBALS['G']['appid'];
		$data['http_host'] = $_SERVER['HTTP_HOST'];
		$respond['status'] = 1;
		echo json_encode($respond);
	}

}


